package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import mx.graphics.codec.JPEGEncoder;
	import mx.graphics.codec.PNGEncoder;
	
	import spark.components.Label;

	/**
	 * PNG转JPG压缩存储
	 */
	public class Png2Jpg
	{
		private static var _ins:Png2Jpg;
		public static function get ins():Png2Jpg
		{
			if(!_ins){
				_ins = new Png2Jpg();
			}
			return _ins;
		}
		
		private var _loader:Loader;
		private var _path:String;
		public var resType:int = 1;
		
		public static var exportUrl:String;
		public static var importRoot:String;
		public static var printMsg:Label;
		
		private var _isJpeg:Boolean;
		
		public function Png2Jpg()
		{
		}
		public function doLoad(path:String):void
		{
			_path = path;
			//			var path:String = "29.mp";
			var request:URLRequest = new URLRequest(path);
			if(!_loader) {
				_loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
//				_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
//				_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError);
//				_loader.addEventListener(Event.OPEN, onLoadOpen);
			}
			_loader.load(request);
		}
		private function onProgress(e:Event):void
		{
			
		}
		private function onComplete(e:Event):void
		{
			if(_loader.contentLoaderInfo.contentType.indexOf("jpeg") != -1){
				_isJpeg = true;	
			}
			else{
				_isJpeg = false;	
			}
			var data:Bitmap = _loader.content as Bitmap;
			var scrBmd:BitmapData = data.bitmapData;
			if(_isJpeg){
				writeImg(bmd, _path);
				return;	
			}
			
			var bmd:BitmapData = new BitmapData(scrBmd.width, scrBmd.height, false, 0);
			
			//复制图片的真实像素区域 
			var p:Point = new Point();
			var rect:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
			bmd.copyPixels(scrBmd, rect, p);
//			writeImg(bmd, _path);
//			return;
			
			var bmd2:BitmapData = new BitmapData(scrBmd.width, scrBmd.height, false, 0);//
			//复制图片的下半区域的红色通道到新图的alpha通道里来实现透明
			bmd2.copyChannel(scrBmd, rect, p, BitmapDataChannel.ALPHA, BitmapDataChannel.RED);
//			bmd2.copyChannel(scrBmd, rect, p, BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
//			var path:String = _path.replace("10", "10-1");
//			writeImg(bmd2, path);
			
//			新红色值 = (旧红色值 * redMultiplier) + redOffset
//			新绿色值 = (旧绿色值 * greenMultiplier) + greenOffset
//			新蓝色值 = (旧蓝色值 * blueMultiplier) + blueOffset
//			新 Alpha 值 = (旧 Alpha 值 * alphaMultiplier) + alphaOffset
			
			var c:ColorTransform = new ColorTransform();
			c.redMultiplier = 1;
			c.greenMultiplier = 1;
			c.blueMultiplier = 1;
			c.redOffset = 100;
			c.greenOffset = 100;
			c.blueOffset = 100;
			bmd2.colorTransform(rect, c);
//			
			var outBmd:BitmapData = new BitmapData(scrBmd.width, scrBmd.height*2);
			outBmd.copyPixels(scrBmd, rect, p);
			p.y = scrBmd.height;
			outBmd.copyPixels(bmd2, rect, p);
			writeImg(outBmd, _path);
			
//			trace("加载完成");
		}
		private function onLoadError(e:Event):void
		{
			
		}
		private function onStatus(e:Event):void
		{
			
		}
		private function onLoadOpen(e:Event):void
		{
			
		}
		
		//------------output=------------------------
		public function writeImg(param1:BitmapData, path:String):void
		{
			var path:String = _path.replace(importRoot, "");
			if(path.indexOf("\\") == 0){
				path = path.substring(1);
			}
			
			var imgByteArray:ByteArray;
			var jpgenc:JPEGEncoder;
			var pngenc:PNGEncoder;
			var bmd:BitmapData = param1;
			var laststr:String;
//			if(_isJpeg)//从json拆分
//			{
				jpgenc = new JPEGEncoder(80);
				imgByteArray = jpgenc.encode(bmd);
				path = path.replace(".png", ".jpg");
//				path = path.replace(".png", ".mp");
				
//				imgByteArray.compress();
//			}
//			else
//			{
//				pngenc = new PNGEncoder();
//				imgByteArray = pngenc.encode(bmd);
//				path = path.replace(".mp", ".png");
//				
//			}
			
			var rootFile:File = new File(exportUrl);
			var fl:File = rootFile.resolvePath(path);
			var fs:FileStream = new FileStream();
			try
			{
				fs.open(fl, FileMode.WRITE);
				fs.writeBytes(imgByteArray);
				fs.close();
			}
			catch(e:Error)
			{
				throw new Error("打包出错：" + path);
			}
			
			printMsg.text = "正在转换，path=" + path;
		}
	}
}